package com.demo.example.hot100;

import java.util.ArrayList;
import java.util.List;

public class GetMaxProfit {
    public static void main(String[] args) {

    }
    //K为最大交易次数
    //M为初始资金
    // 交易次数K ，指的是能够买入K次，出手K次
    //莽夫流解法，每到一支股票，就选择收或者出
    public static double getMaxProfit(double M,double N,double[] historyPrices,int K){
        List<Double> list=new ArrayList<>();
        process(M,historyPrices,0,K,list,0);
        double max=Double.MIN_VALUE;
        for(int i=0;i<list.size();i++){
            max=list.get(i)>max?list.get(i):max;
        }
        return max-M;
    }
    public static void process(double now, double[] historyPrices, int index, int K, List<Double> profits,double num){
        //num为数量
        if(K==0){
            profits.add(now);
            return;
        }
        if(index>=historyPrices.length){
            return;
        }
        //我们手头上目前有资金
        if(now!=0){
            //要当前的股票
            process(0,historyPrices,index+1,K,profits,now/historyPrices[index]);
            //不要当前的股票
            process(now,historyPrices,index+1,K,profits,0);
        }else{
            //如果now不为0，我们只能选择要或者不要当前的股票
            //卖出去当前的股票
            process(num*historyPrices[index],historyPrices,index+1,K-1,profits,0);
            //不卖出去当前的股票
            process(0,historyPrices,index+1,K,profits,num);
        }

    }

}
